package where2go.sctu.com.zhiyou.adpater;

import android.content.Context;
import android.graphics.Bitmap;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.ImageRequest;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import where2go.sctu.com.zhiyou.MyApplication;
import where2go.sctu.com.zhiyou.R;
import where2go.sctu.com.zhiyou.db.MyStudyHoder;
import where2go.sctu.com.zhiyou.entity.StudyData;
import where2go.sctu.com.zhiyou.entity.User;

/**
 * Created by lenovo on 2018/5/28.
 */

public class StudyAdaper extends RecyclerView.Adapter<MyStudyHoder> {
    private SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
    private String url= "http://47.104.80.155/youyiyou/resources/classImg/";
    private Context context;
    private List<StudyData.DataBean> dataLists = new ArrayList<StudyData.DataBean>();
    private List<User> userList = new ArrayList<User>();

    public interface OnItemClickListener{
        void onItemClick(View view,int position);
        void onItemLongClick(View view,int position);
    }

    private StudyAdaper.OnItemClickListener mOnItemClickListener;

    public void setItemClickListener(StudyAdaper.OnItemClickListener listener) {
        this.mOnItemClickListener=listener;
    }

    public StudyAdaper(Context context, List<StudyData.DataBean> dataList) {
        this.context = context;
        this.dataLists = dataList;
    }

    @Override
    public MyStudyHoder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(context).inflate(R.layout.my_study, parent, false);
        MyStudyHoder holdered = new MyStudyHoder(view);
        return holdered;
    }

    @Override
    public void onBindViewHolder(final MyStudyHoder holder, final int position) {

        StudyData.DataBean.AClassBean classData=dataLists.get(position).getAClass();
        holder.name.setText(classData.getClassTitle());
        holder.content.setText(classData.getClassDetail());
        String s=format.format(new Date(Long.parseLong(String.valueOf(classData.getClassTime()))));
        holder.time.setText(s);

        if (classData.getClassPrice()!=null){
            holder.price.setText("￥ "+String.valueOf(classData.getClassPrice())+".00");
        }else{
            holder.price.setText("￥ 0.00");
        }

        ImageRequest imageRequest = new ImageRequest(url + dataLists.get(position).getAClass().getClassImg(),
                new Response.Listener<Bitmap>() {
                    @Override
                    public void onResponse(Bitmap bitmap) {
                        holder.image.setImageBitmap(bitmap);
                    }
                }, 0, 0, Bitmap.Config.ARGB_8888, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                //请求失败 设置默认图片

                holder.image.setImageResource(R.mipmap.ic_launcher);
            }

        });
        MyApplication.getHttpQueues().add(imageRequest);

        if (mOnItemClickListener!=null){
            holder.itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    mOnItemClickListener.onItemClick(holder.itemView,position);
                }
            });

            holder.itemView.setOnLongClickListener(new View.OnLongClickListener() {
                @Override
                public boolean onLongClick(View v) {
                    mOnItemClickListener.onItemLongClick(holder.itemView,position);
                    return false;
                }
            });
        }
    }

    @Override
    public int getItemCount() {
        return dataLists != null ? dataLists.size() : 0;
    }

}
